// pages/tzApplication/tzApplication.js

var areaList = require("../../libs/area.js");
var core = require("../../utils/core.js");
import Notify from '../../components/dist/notify/notify';
import Toast from '../../components/dist/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageState: 0, //0表单状态   1申请中状态
    name: '',
    areaList: '',
    xqName: '', //小区名称
    xxName:'',//详细地址
    phoneNum: '', //手机号
    IDcard: '', //身份证号码
    areashow: false,
    datePickershow: true,
    areaStr: '请选择地址', //选择的地址
    currentDate: new Date().getTime(),
    minDate: new Date(1920).getTime(),
    Choosedate: '', //选择的时间
    OccupationFlag: false,
    choosedOccup: '请选择职业', //选择的职业
    actions: [{
        name: '厨师',
      },
      {
        name: '教师',

      },
      {
        name: '职员',

      },
      {
        name: '其他',

      }
    ],
    yzmNum: '', //输入的验证码
    canSubmFlag: false, //按钮显示是否可以提交
    endtime: 60, //倒计时时间
    endtimeStr: '获取验证码', //倒计时按钮文字
    endTimer: '', //倒计时定时器

  },
  checkSubmitCansuB: function() { //检查是否可以提交
    if (this.data.name == '') {
      this.showNotify('请输入名字');
      return false;
    } else if (this.data.areaStr == '请选择地址') {
      this.showNotify('请请选择地址');
      return false;
    } else if (this.data.xqName == '') {
      this.showNotify('请输入小区名字');
      return false;
    } else if (this.data.xxName == '') {
      this.showNotify('请输入详细地址');
      return false;
    } else if (this.data.phoneNum == '') {
      this.showNotify('请输入手机号');
      return false;
    } else if (this.data.choosedOccup == '请选择职业') {
      this.showNotify('请选择职业');
      return false;
    } else if (this.data.yzmNum == '') {
      this.showNotify('请输入验证码');
      return false;
    } else if (this.data.IDcard == '') {
      this.showNotify('请输入身份证号码');
      return false;
    } else {
      return true
    }
  },
  checkSubmitBtn: function() { //检查是否可以提交,按钮状态
    if (this.data.name == '') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else if (this.data.areaStr == '请选择地址') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else if (this.data.xqName == '') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else if (this.data.xxName == '') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else if (this.data.phoneNum == '') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else if (this.data.choosedOccup == '请选择职业') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else if (this.data.IDcard == '') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else if (this.data.yzmNum == '') {
      this.setData({
        canSubmFlag: false
      })
      return
    } else {
      this.setData({
        canSubmFlag: true
      })
    }
  },

  showNotify: function(str) {
    Notify({
      text: str,
      duration: 1500,
      selector: '#custom-selector',
      backgroundColor: '#f07d60'
    });
  },
  goSubmit: function() {
    if (!this.checkSubmitCansuB()) {
      return
    } else {
      // address 地址
      // homename 小区名称
      // username 团长名称
      // mobile 手机
      // idcard 身份证号码
      // career 职业  验证码 code
      var s = {};
      s.address = this.data.areaStr;
      s.homename = this.data.xqName;
      s.address_info = this.data.xxName;
      s.username = this.data.name;
      s.mobile = this.data.phoneNum;
      s.idcard = this.data.IDcard;
      s.career = this.data.choosedOccup;
      s.code = this.data.yzmNum;
      core.get("groupapp/master/apply_master", s, function(e) {
        console.log(e);
        if(e.error==0){
          Toast.success('申请成功');
          

        }else{
          Toast.success('申请失败');
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var that = this;
    that.setData({
      areaList: core.getareaList()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  onNameChange: function(e) {
    var str = e.detail.value
    if (str) {
      this.setData({
        name: str
      })
    }
    this.checkSubmitBtn()
  },
  onXQNameChange: function(e) {

    var str = e.detail.value
    if (str) {
      this.setData({
        xqName: str
      })
    }
    this.checkSubmitBtn()
  },
  onxxNameChange:function(e){
    var str = e.detail.value
    if (str) {
      this.setData({
        xxName: str
      })
    }
    this.checkSubmitBtn()
  },



  showAreaSelectd: function() {
    this.setData({
      areashow: true
    })
  },
  dateChange: function(e) {

  },
  earaChange: function(e) {
    // console.log(e);
  },
  datecancel: function() {
    this.setData({
      datePickershow: false
    })
  },
  dateconfirm: function(e) {


    this.setData({
      datePickershow: false
    })
  },
  earaconfirm: function(e) { //确定
    // console.log(e);
    console.log(e.detail.values);
    var arr = e.detail.values;
    var str = '';
    for (var i = 0; i < arr.length; i++) {
      if (i < arr.length - 1) {
        str += arr[i].name;
        str += '-'
      } else {
        str += arr[i].name;
      }
    }
    this.setData({
      areaStr: str,
      areashow: false
    })
    this.checkSubmitBtn()
  },
  earacancel: function() { //取消
    this.setData({
      areashow: false
    })
  },
  onPhoneChange: function(e) { //手机输入框，失去焦点获取手机号存起来
    var num = e.detail.value;
    if (!(/^1[34578]\d{9}$/.test(num))) {
      this.showNotify('请输入正确的手机号');
      this.setData({
        phoneNum: ''
      })
      return
    }
    this.setData({
      phoneNum: num
    })
    this.checkSubmitBtn()
  },
  onIDcardChange: function(e) { //身份证输入框，失去焦点获取手机号存起来

    var num = e.detail.value;
    if (!core.isCardNo(num)) {
      this.showNotify('请输入正确的身份证号码');
      this.setData({
        IDcard: ''
      })
      return
    }

    this.setData({
      IDcard: num
    })
    this.checkSubmitBtn()
  },
  showSelect: function() { //显示选择职业
    this.setData({
      OccupationFlag: true
    })
  },
  onOccupationClose: function() {
    this.setData({
      OccupationFlag: false
    })
  },
  onOccupationSelect: function(e) {

    var str = e.detail.name;
    this.setData({
      OccupationFlag: false,
      choosedOccup: str
    })
    this.checkSubmitBtn()
  },
  onSMSChange: function(e) {
    console.log('保存验证码');
    var str = e.detail.value;
    this.setData({
      yzmNum: str
    })
    this.checkSubmitBtn()
  },
  StartendTime: function() { //开始倒计时
    var that = this;
    // endtime: 60,//倒计时时间
    // endtimeStr: '获取验证码',//倒计时按钮文字
    //  endTimer:'',//倒计时定时器
    this.data.endTimer = setInterval(function() {
      if (that.data.endtime > 0) {
        var curtime = that.data.endtime - 1
        that.setData({
          endtime: curtime,
          endtimeStr: curtime + 's'
        })
      } else if (that.data.endtime == 0) {
        clearInterval(that.data.endTimer)
        that.setData({
          endtimeStr: '获取验证码',
          endtime: 60,
        })
      }

    }, 1000)


  },
  getSMSCode: function() { //获取验证码
    var that = this;
    var phoneNum = that.data.phoneNum;
    if (!(/^1[34578]\d{9}$/.test(phoneNum))) {
      this.showNotify('请输入正确的手机号');
      this.setData({
        phoneNum: ''
      })
      return
    }
    if (that.data.endtime > 0 && that.data.endtime < 60) {
      that.showNotify('操作太频繁');
      return
    }
    this.StartendTime();
    return
    if (this.data.phoneNum == '') {
      this.showNotify('请输入手机号');
    } else if (!(/^1[34578]\d{9}$/.test(this.data.phoneNum))) {
      this.showNotify('请输入正确的手机号');
    } else {
      console.log('发送验证码');
      core.get("sms/changemobile", {
        mobile: this.data.phoneNum
      }, function(e) {
        0 == e.error ? (Toast("短信发送成功"), that.setData({
          endtime: 60
        }), that.StartendTime()) : Toast(e.message);
      }, function(err) {

      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  gohome: function() {
    console.log(123)
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})